package edu.iss.demo.web.controller;


import java.util.Date;
import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.iss.demo.model.Employee;
import edu.iss.demo.model.PublicHoliday;
import edu.iss.demo.service.EmployeeService;
import edu.iss.demo.service.PublicHolidayService;


@Controller
public class AdminControllerG 
{
	@Autowired
	private PublicHolidayService pservice;
	
	@Autowired
	private EmployeeService eservice;
	
	@RequestMapping(value = "/admin/manage-publicHoliday", method = RequestMethod.GET)
	public ModelAndView viewPublicHoliday()
	{
		return new ModelAndView("admin_recordHoliday");
	}
	@RequestMapping(value="/admin/publicHoliday",method=RequestMethod.POST,params="update")
	public ModelAndView enterLeaveType(@ModelAttribute("SpringWeb")PublicHoliday ph,final RedirectAttributes rdr,  WebRequest webRequest)
	{
		ModelAndView  mav=new ModelAndView();
		String dateStr = webRequest.getParameter("dtpHoliday");
		
		try
		{
			SimpleDateFormat frmt=new SimpleDateFormat("yyyy-MM-dd");
			Date changedDate=frmt.parse(dateStr);
			ph.setDate(changedDate);
			PublicHoliday pnew=pservice.create(ph);
			mav.setViewName("redirect:/admin/manage-publicHoliday");
			String msg="New Public Holiday"+pnew.getDescription() +" was created successfully";
			rdr.addFlashAttribute("msg", msg);
			return mav;	
		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mav;
			
	}
	@RequestMapping(value="/admin/publicHoliday",method=RequestMethod.POST,params="delete")
	public ModelAndView clearLeave()
	{
		ModelAndView mav=new ModelAndView();
		mav.setViewName("redirect:/admin/manage-publicHoliday");
		return mav;	
	}
	
	@RequestMapping(value = "/admin/managestaff", method = RequestMethod.GET)
	public ModelAndView vieLeaveType(@ModelAttribute("SpringWeb")Employee em, ModelMap model, WebRequest webRequest,final RedirectAttributes redirectAttributes)
	{
		return new ModelAndView("admin_createStaff");
	}
	
	@RequestMapping(value="/admin/createstaff",method=RequestMethod.POST,params="save")
	public ModelAndView enterStaff(@ModelAttribute("SpringWeb")Employee em, ModelMap model, WebRequest webRequest,final RedirectAttributes redirectAttributes)
	{
		String dobTran = webRequest.getParameter("spDob");
		String hireTran = webRequest.getParameter("spDateHired");
		try
		{
			SimpleDateFormat frmt=new SimpleDateFormat("yyyy-MM-dd");
			Date changedDob=frmt.parse(dobTran);
			Date changedHiredDate=frmt.parse(hireTran);
			em.setDob(changedDob);em.setDateHired(changedHiredDate);
		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ModelAndView mav = new ModelAndView();
		eservice.create(em);	
		mav.setViewName("redirect:/admin/managestaff");
		String message = "New Staff " + em.getName()
				+ " was successfully created.";

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	
	@RequestMapping(value="/admin/createstaff",method=RequestMethod.POST,params="delete")
	public ModelAndView enterStaff( ModelMap model,final RedirectAttributes redirectAttributes)
	{
		ModelAndView mav=new ModelAndView();
		mav.setViewName("redirect:/admin/managestaff");
		return mav;
	}
	@RequestMapping(value="/admin/view-public-Holidays",method=RequestMethod.GET)
	public ModelAndView viewPH()
	{
		ModelAndView mav=new ModelAndView("admin_recordHolidayListing");
		List<PublicHoliday> hlist=pservice.findAll();
		mav.addObject("hlist", hlist);
		return mav;	
	}
	
			
	

}